
"""
    设备表
"""


from django.db import models
from .asste import AssetModel


class ServerModel(models.Model):

    """
        服务器设备
    """

    sub_asset_type_choice = (
        (0, 'PC服务器'),
        (1, '刀片机'),
        (2, '小型机'),
    )

    created_by_choice = (
        ('auto', '自动添加'),
        ('manual', '手工录入'),
    )

    asset = models.OneToOneField(AssetModel, on_delete=models.CASCADE)  # 非常关键的一对一关联！asset被删除的时候一并删除server
    sub_asset_type = models.SmallIntegerField(choices=sub_asset_type_choice, default=0, verbose_name="服务器类型")
    created_by = models.CharField(choices=created_by_choice, max_length=32, default='auto', verbose_name="添加方式")
    hosted_on = models.ForeignKey('self', related_name='hosted_on_server', blank=True, null=True,
                                  verbose_name="宿主机", on_delete=models.CASCADE)  # 虚拟机专用字段
    model = models.CharField(max_length=128, null=True, blank=True, verbose_name='服务器型号')
    raid_type = models.CharField(max_length=512, blank=True, null=True, verbose_name='Raid类型')

    os_type = models.CharField(max_length=64, blank=True, null=True, verbose_name=u'操作系统类型')
    os_distribution = models.CharField(max_length=64, blank=True, null=True, verbose_name=u'发行商')
    os_release = models.CharField(max_length=64, blank=True, null=True, verbose_name=u'操作系统版本')
    time = models.DateTimeField(auto_now_add=True, verbose_name=u'创建日期')

    def __str__(self):
        return self.asset.name

    class Meta:
        app_label = 'cmdb'
        ordering = ['-time']
        verbose_name = u'服务器'
        verbose_name_plural = verbose_name


class SecurityModel(models.Model):

    """
        安全设备
    """

    sub_asset_type_choice = (
        (0, '防火墙'),
        (1, '入侵检测设备'),
        (2, '互联网网关'),
        (4, '运维审计系统'),
    )

    asset = models.OneToOneField(AssetModel, on_delete=models.CASCADE)
    sub_asset_type = models.SmallIntegerField(choices=sub_asset_type_choice, default=0, verbose_name="安全设备类型")
    model = models.CharField(max_length=128, default='未知型号', verbose_name='安全设备型号')
    time = models.DateTimeField(auto_now_add=True, verbose_name=u'创建日期')

    def __str__(self):
        return self.asset.name

    class Meta:
        app_label = 'cmdb'
        ordering = ['-time']
        verbose_name = u'安全设备'
        verbose_name_plural = verbose_name


class StorageModel(models.Model):

    """
        存储设备
    """

    sub_asset_type_choice = (
        (0, '磁盘阵列'),
        (1, '网络存储器'),
        (2, '磁带库'),
        (4, '磁带机'),
    )

    asset = models.OneToOneField(AssetModel, on_delete=models.CASCADE)
    sub_asset_type = models.SmallIntegerField(choices=sub_asset_type_choice, default=0, verbose_name="存储设备类型")
    model = models.CharField(max_length=128, default='未知型号', verbose_name='存储设备型号')
    time = models.DateTimeField(auto_now_add=True, verbose_name=u'创建日期')

    def __str__(self):
        return self.asset.name

    class Meta:
        app_label = 'cmdb'
        ordering = ['-time']
        verbose_name = u'存储设备'
        verbose_name_plural = verbose_name


class NetworkModel(models.Model):

    """
        网络设备
    """

    sub_asset_type_choice = (
        (0, '路由器'),
        (1, '交换机'),
        (2, '负载均衡'),
        (4, 'VPN设备'),
        (5, '未知型号'),
    )

    asset = models.OneToOneField(AssetModel, on_delete=models.CASCADE)
    sub_asset_type = models.SmallIntegerField(choices=sub_asset_type_choice, default=0, verbose_name="网络设备类型")
    vlan_ip = models.GenericIPAddressField(blank=True, null=True, verbose_name="VLanIP")
    intranet_ip = models.GenericIPAddressField(blank=True, null=True, verbose_name="内网IP")
    model = models.CharField(max_length=128, default=5,  verbose_name="网络设备型号")
    firmware = models.CharField(max_length=128, blank=True, null=True, verbose_name="设备固件版本")
    port_num = models.SmallIntegerField(null=True, blank=True, verbose_name="端口个数")
    device_detail = models.TextField(null=True, blank=True, verbose_name="详细配置")
    time = models.DateTimeField(auto_now_add=True, verbose_name=u'创建日期')

    def __str__(self):
        return self.asset.name

    class Meta:
        app_label = 'cmdb'
        ordering = ['-time']
        verbose_name = u'网络设备'
        verbose_name_plural = verbose_name

